package factory.demo2;

import java.util.HashMap;
import java.util.Map;

/**
 * 用简单的方法实现工厂的工厂,作用是不在需要每次创建新的工厂对象
 *
 * @author 1
 * @version 1.0
 * @description: TODO
 * @date 2024-08-28 14:56
 */
public class FreeGoodsFactoryMap {
    private static final Map<Integer, FreeGoodsFactory> cachedFactories = new HashMap<Integer, FreeGoodsFactory>();

    /**
     * 静态代码块初始化工厂方法
     */
    static {
        cachedFactories.put(1, new DiscountFreeGoodsFactory());
        cachedFactories.put(2, new SmallGiftFreeGoodsFactory());
    }

    public static FreeGoodsFactory getParserFactory(Integer type) {
        if (type == 1) {
            return cachedFactories.get(1);
        } else if (type == 2) {
            return cachedFactories.get(2);
        }
        return null;
    }


}
